package net.esj.basic.dao.jdbc.jpa.support;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;

import net.esj.basic.dao.jdbc.jpa.ConnectionProvider;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 基于驱动的连接提供类。
 *
 *
 */
public class DriverConnectionProvider implements ConnectionProvider{
    private static Log log = LogFactory.getLog(DriverConnectionProvider.class);

    private String url;
    private String user;
    private String password;
    private String driver;

    public final Connection getConnection(){
			try {
				Class.forName(driver);
				return DriverManager.getConnection(
						url,
						user,
						password);
			} catch (ClassNotFoundException e) {
				throw new RuntimeException(e.getMessage(),e);
			} catch (SQLException e) {
				throw new RuntimeException(e.getMessage(),e);
			}

	}
	public String getDriver() {
		return driver;
	}
	public void setDriver(String driver) {
		this.driver = driver;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	/**
	 * 属性名和miniorm.cfg.xml里要事先约定好。
	 */
	public void init(Map properties) {
		this.driver = (String) properties.get("connection.driver");
		this.user = (String) properties.get("connection.user");
		this.url = (String) properties.get("connection.url");
		this.password = (String) properties.get("connection.password");
	}

    
}

